package com.situ.mall.web.controller;


import com.situ.mall.common.entity.UserBaseInfo;
import com.situ.mall.common.entity.Worker;
import com.situ.mall.common.exception.MyException;
import com.situ.mall.common.mapper.WorkerMapper;
import com.situ.mall.common.service.IWorkerService;
import com.situ.mall.common.service.impl.LoginServiceImpl;
import com.situ.mall.common.util.StringUtil;
import com.situ.mall.common.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-01-29
 */
@RestController
@RequestMapping("/client")
public class WorkerController {

    @Autowired
    private IWorkerService workerService;

    @Autowired
    private WorkerMapper workerMapper;

    //首页查询推荐陪诊员
    @GetMapping("/worker/indexWorkers")
    public ResultVO indexWorkers() {
        return workerService.indexWorkers();
    }

    //注册成为陪诊员
    @PostMapping("/worker/register")
    public ResultVO register(Worker worker) {
        // 1.检查手机号格式
        StringUtil.checkTel(worker.getTel());

        // 2.再根据手机号去查陪诊员，一个用户账户只能注册一个陪诊员账号
        if (workerMapper.selectByTel(worker.getTel()) != null) {
            throw new MyException("手机号已注册！");
        } else if (workerMapper.selectByMemberId(worker.getMemberId()) != null) {
            throw new MyException("该账号已经注册过！");
        } else {

            //3.如果没被注册，继续
            //随机一个昵称，和背景图
            UserBaseInfo info = LoginServiceImpl.setUserInfo(worker.getAvatar(), worker.getNickname());
            worker.setNickname(info.getNickname());
            worker.setBackgroundPic(info.getBackgroundPic());
            worker.setStatus("0");
            worker.setPassword(StringUtil.md5(worker.getPassword()));
            //保存到数据库
            workerMapper.register(worker);
            //再查一遍
            Worker currentUser = workerMapper.selectByTel(worker.getTel());
            return ResultVO.data(200, "请求成功", currentUser);
        }
    }

    //编辑个人资料
    @PostMapping("/worker/changeWorkerInfo")
    public ResultVO updateInfo(Worker worker) {
        System.out.println(worker);

        //1.判断手机号格式是否正确
        StringUtil.checkTel(worker.getTel());

        //2.判断手机号是否被使用
        // 先根据传过来的用户的手机号去数据库查，如果查到了，把查到的用户id和传过来的用户id作比较，如果一致就继续，如果不一致就报错
        Worker w = workerMapper.selectByTel(worker.getTel());
        if(w != null) {
            if (!w.getId().equals(worker.getId())) {
                throw new MyException("手机号已被注册");
            }
        }

        //3.判断身份证号格式是否正确（非空时才判断）
        if(StringUtil.isNotBlank(worker.getIdCardNumber())) {
            StringUtil.checkIdCardNumber(worker.getIdCardNumber());
        }

        workerMapper.updateInfo(worker);

        //再查一遍，返回
        return ResultVO.data(200, "请求成功", workerService.getById(worker.getId()));
    }

    //查询当前登录用户注册的陪诊员账号
    @GetMapping("/worker/selectIndexWorker")
    public ResultVO selectIndexWorker(Integer memberId) {
        return ResultVO.data(200, "请求成功", workerMapper.selectByMemberId(memberId));
    }

}
